﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.Test
{
    public partial class frmMT : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        private DataSet ds;
        private string _madethi;
        private int _thoigianlambai, _listen, _read;

        public frmMT(string madethi, string thoigianlambai)
        {
            InitializeComponent();
            this._madethi = madethi;
            this._thoigianlambai = Convert.ToInt32(thoigianlambai);
        }

        private void frmMT_Load(object sender, EventArgs e)
        {
            ds = new DataSet();

            slook_Enquiry.Enabled = true;
            LoadEnquiry();
            slook_Enquiry.Properties.DataSource = ds.Tables["TableEnquiry"];
            slook_Enquiry.Properties.DisplayMember = "FullName";
            slook_Enquiry.Properties.ValueMember = "EnquiryNumber";

            pnl_Main.VerticalScroll.Enabled = true;
            AddFile(String.Format("{0}.rtf", _madethi));

            txtTestDate.Enabled = false;
            btn_Start.Enabled = true;
            btn_Finish.Enabled = false;
            timerTest.Stop();
            txtTestDate.EditValue = DateTime.Now.ToShortDateString();

            //I/ Listening: Circle the correct letter.
            Label lbI = AddLabel(this.pnl_Main, "lbI", "I/ Listening (10 marks).", Color.Red, pnl_Listening.Bottom);

            //I.1
            AddListening1(1, 10, lbI.Location.Y);
        }

        #region "Load de thi"
        private void AddFile(string filename)
        {
            try
            {
                pnl_Listening.Visible = true;
                pnl_Listening.Width = 1024;
                pnl_Listening.Height = 800;
                rich_Listening.LoadFile(Environment.CurrentDirectory + "/" + filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddListening1(int begin, int end, int y)
        {
            try
            {
                y += 20;
                Label lbI1 = AddLabel(this.pnl_Main, "lbI1", "1/ Listen and circle the correct answers.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupI1 = new Panel();
                    groupI1.Location = new Point(10, y + 20);
                    groupI1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupI1.AutoSize = true;
                    groupI1.Name = "grpI1" + index;
                    this.pnl_Main.Controls.Add(groupI1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupI1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtI1" + index;
                    this.pnl_Main.Controls[groupI1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //y = AddABCD(this.pnl_Main, begin, end, y);

                //Add I.2
                AddListening2(1, 5, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddListening2(int begin, int end, int y)
        {
            try
            {
                y += 50;
                Label lbI2 = AddLabel(this.pnl_Main, "lbI2", "2/ Listen and answer true (T) or false (F). ", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupI2 = new Panel();
                    groupI2.Location = new Point(10, y + 20);
                    groupI2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupI2.AutoSize = true;
                    groupI2.Name = "grpI2" + index;
                    this.pnl_Main.Controls.Add(groupI2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupI2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtI2" + index;
                    this.pnl_Main.Controls[groupI2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add II Level 1
                Label lbII = AddLabel(this.pnl_Main, "lbII", "II/ Level 1 (10 marks).", Color.Red, y + 50);

                AddLevel1(1, 10, lbII.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel1(int begin, int end, int y)
        {
            try
            {
                y = AddABCD(this.pnl_Main, begin, end, y, "II0");

                //Add III2 Level 2
                Label lbIII = AddLabel(this.pnl_Main, "lbIII", "III/ Level 2 (10 marks)", Color.Red, y + 50);
                AddLevel2A(1, 7, lbIII.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel2A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbIII1 = AddLabel(this.pnl_Main, "lbIII1", "A.  Match the sentences (1–7) with the answers (a–h).", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupIII1 = new Panel();
                    groupIII1.Location = new Point(10, y + 20);
                    groupIII1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupIII1.AutoSize = true;
                    groupIII1.Name = "grpIII1" + index;
                    this.pnl_Main.Controls.Add(groupIII1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupIII1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtIII1" + index;
                    this.pnl_Main.Controls[groupIII1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add III Level 2 B

                AddLevel2B(8, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel2B(int begin, int end, int y)
        {
            try
            {
                y += 50;
                Label lbIII2 = AddLabel(this.pnl_Main, "lbIII2", "B. Read the texts and answer the questions. ", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupIII2 = new Panel();
                    groupIII2.Location = new Point(10, y + 20);
                    groupIII2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupIII2.AutoSize = true;
                    groupIII2.Name = "grpIII2" + index;
                    this.pnl_Main.Controls.Add(groupIII2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupIII2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtIII2" + index;
                    this.pnl_Main.Controls[groupIII2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add IV Level 3
                Label lbIV = AddLabel(this.pnl_Main, "lbIV", "IV/ Level 3 (10 marks).", Color.Red, y + 50);
                //Add IV level 3A
                AddLevel3A(1, 5, lbIV.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel3A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbIV1 = AddLabel(this.pnl_Main, "lbIV1", "A. Correct the mistakes with the correct adjective or adverb.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupIV1 = new Panel();
                    groupIV1.Location = new Point(10, y + 20);
                    groupIV1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupIV1.AutoSize = true;
                    groupIV1.Name = "grpIV1" + index;
                    this.pnl_Main.Controls.Add(groupIV1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupIV1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 100;
                    txt1.Name = "txtIV1" + index;
                    this.pnl_Main.Controls[groupIV1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add IV Level 3B

                AddLevel3B(6, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel3B(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbIV2 = AddLabel(this.pnl_Main, "lbIV2", "B.	Complete the text with the past form of the verbs below.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupIV2 = new Panel();
                    groupIV2.Location = new Point(10, y + 20);
                    groupIV2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupIV2.AutoSize = true;
                    groupIV2.Name = "grpIV2" + index;
                    this.pnl_Main.Controls.Add(groupIV2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupIV2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 200;
                    txt1.Name = "txtIV2" + index;
                    this.pnl_Main.Controls[groupIV2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add V Level 4
                Label lbV = AddLabel(this.pnl_Main, "lbV", "V/ Level 4 (10 marks).", Color.Red, y + 50);
                //Add V Level 4A
                AddLevel4A(1, 5, lbV.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel4A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbV1 = AddLabel(this.pnl_Main, "lbV1", "A. Complete the sentences with the correct form of the verbs.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupV1 = new Panel();
                    groupV1.Location = new Point(10, y + 20);
                    groupV1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupV1.AutoSize = true;
                    groupV1.Name = "grpV1" + index;
                    this.pnl_Main.Controls.Add(groupV1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupV1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 200;
                    txt1.Name = "txtV1" + index;
                    this.pnl_Main.Controls[groupV1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add V Level 4 B

                AddLevel4B(6, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel4B(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbV2 = AddLabel(this.pnl_Main, "lbV2", "B.	Read and answer true (T) or false (F).", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupV2 = new Panel();
                    groupV2.Location = new Point(10, y + 20);
                    groupV2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupV2.AutoSize = true;
                    groupV2.Name = "grpV2" + index;
                    this.pnl_Main.Controls.Add(groupV2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupV2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtV2" + index;
                    this.pnl_Main.Controls[groupV2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add VI Level 5
                Label lbVI = AddLabel(this.pnl_Main, "lbVI", "VI/ Level 5 (10 marks).", Color.Red, y + 50);
                //Add VI Level 5A
                AddLevel5A(1, 4, lbVI.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel5A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbVI1 = AddLabel(this.pnl_Main, "lbVI1", "A.	Write the exclamations under the correct heading.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupVI1 = new Panel();
                    groupVI1.Location = new Point(10, y + 20);
                    groupVI1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupVI1.AutoSize = true;
                    groupVI1.Name = "grpVI1" + index;
                    this.pnl_Main.Controls.Add(groupVI1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupVI1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtVI1" + index;
                    this.pnl_Main.Controls[groupVI1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add VI Level 5 B

                AddLevel5B(5, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel5B(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbVI2 = AddLabel(this.pnl_Main, "lbVI2", "B. Order the dialogue. Write it out with the correct names.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupVI2 = new Panel();
                    groupVI2.Location = new Point(10, y + 20);
                    groupVI2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupVI2.AutoSize = true;
                    groupVI2.Name = "grpVI2" + index;
                    this.pnl_Main.Controls.Add(groupVI2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupVI2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 200;
                    txt1.Name = "txtVI2" + index;
                    this.pnl_Main.Controls[groupVI2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add VII Level 6
                Label lbVII = AddLabel(this.pnl_Main, "lbVII", "VII/ Level 6 (10 marks).", Color.Red, y + 50);
                //Add VII Level 6A
                AddLevel6A(1, 5, lbVII.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel6A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbVII1 = AddLabel(this.pnl_Main, "lbVII1", "A.	Correct the mistake in each sentence.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupVII1 = new Panel();
                    groupVII1.Location = new Point(10, y + 20);
                    groupVII1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupVII1.AutoSize = true;
                    groupVII1.Name = "grpVII1" + index;
                    this.pnl_Main.Controls.Add(groupVII1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupVII1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 300;
                    txt1.Name = "txtVII1" + index;
                    this.pnl_Main.Controls[groupVII1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add VII Level 6 B

                AddLevel6B(6, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel6B(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbVII2 = AddLabel(this.pnl_Main, "lbVII2", "B.	Your friends have a lot of problems. Use the prompts to give them advice.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupVII2 = new Panel();
                    groupVII2.Location = new Point(10, y + 20);
                    groupVII2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupVII2.AutoSize = true;
                    groupVII2.Name = "grpVII2" + index;
                    this.pnl_Main.Controls.Add(groupVII2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupVII2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtVII2" + index;
                    this.pnl_Main.Controls[groupVII2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add VIII Level 7
                Label lbVIII = AddLabel(this.pnl_Main, "lbVIII", "VIII/ Level 7 (10 marks).", Color.Red, y + 50);
                //Add VIII Level 7A
                AddLevel7A(1, 5, lbVIII.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel7A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbVIII1 = AddLabel(this.pnl_Main, "lbVIII1", "A.	Complete the sentences with one word in each gap.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupVIII1 = new Panel();
                    groupVIII1.Location = new Point(10, y + 20);
                    groupVIII1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupVIII1.AutoSize = true;
                    groupVIII1.Name = "grpVIII1" + index;
                    this.pnl_Main.Controls.Add(groupVIII1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupVIII1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 200;
                    txt1.Name = "txtVIII1" + index;
                    this.pnl_Main.Controls[groupVIII1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add VIII Level 7 B

                AddLevel7B(6, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel7B(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbVIII2 = AddLabel(this.pnl_Main, "lbVIII2", "B	Complete the sentences with the correct form of the verbs in brackets.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupVIII2 = new Panel();
                    groupVIII2.Location = new Point(10, y + 20);
                    groupVIII2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupVIII2.AutoSize = true;
                    groupVIII2.Name = "grpVIII2" + index;
                    this.pnl_Main.Controls.Add(groupVIII2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupVIII2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 300;
                    txt1.Name = "txtVIII2" + index;
                    this.pnl_Main.Controls[groupVIII2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add IX Level 8
                Label lbIX = AddLabel(this.pnl_Main, "lbIX", "IX/ Level 8 (10 marks).", Color.Red, y + 50);
                //Add IX Level 8A
                AddLevel8A(1, 5, lbIX.Location.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel8A(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbIX1 = AddLabel(this.pnl_Main, "lbIX1", "A.  Read the article and choose the correct sentences (b-f) to complete the gaps.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupIX1 = new Panel();
                    groupIX1.Location = new Point(10, y + 20);
                    groupIX1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupIX1.AutoSize = true;
                    groupIX1.Name = "grpIX1" + index;
                    this.pnl_Main.Controls.Add(groupIX1);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupIX1.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 20;
                    txt1.Name = "txtIX1" + index;
                    this.pnl_Main.Controls[groupIX1.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add IX Level 8B

                AddLevel8B(6, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddLevel8B(int begin, int end, int y)
        {
            try
            {
                y += 50;

                Label lbIX2 = AddLabel(this.pnl_Main, "lbIX2", "B.  Read the article again and complete the summary.", Color.Blue, y);

                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupIX2 = new Panel();
                    groupIX2.Location = new Point(10, y + 20);
                    groupIX2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupIX2.AutoSize = true;
                    groupIX2.Name = "grpIX2" + index;
                    this.pnl_Main.Controls.Add(groupIX2);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    this.pnl_Main.Controls[groupIX2.Name].Controls.Add(lb);

                    TextBox txt1 = new TextBox();
                    txt1.Location = new Point(lb.Location.X + 20, lb.Location.Y);
                    txt1.Width = 300;
                    txt1.Name = "txtIX2" + index;
                    this.pnl_Main.Controls[groupIX2.Name].Controls.Add(txt1);
                    txt1.TextChanged += new EventHandler(TextBox_TextChanged);

                    y += 20;
                }

                //Add III Level 2 B

                //AddLevel2B(8, 10, y);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region "Load Other"
        private void LoadEnquiry()
        {
            try
            {
                string sql = String.Format(@"select EnquiryNumber, (Holot + ' ' + Ten) as 'FullName'
                                            from Enquiry");
                SqlDataAdapter da = new SqlDataAdapter(sql, GlobalVariable.WSInfo.ConnectionString);
                da.Fill(ds, "TableEnquiry");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadMusic(string filename)
        {
            try
            {
                axWindowsMediaPlayer1.URL = Environment.CurrentDirectory + "/" + filename;
                axWindowsMediaPlayer1.Ctlcontrols.play();
                axWindowsMediaPlayer1.Ctlenabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadTime()
        {
            try
            {
                progressBarTime.Maximum = _thoigianlambai;
                progressBarTime.Value = progressBarTime.Maximum;

                lblTimeLeft.Text = "Time left (minutes): " + progressBarTime.Value.ToString();

                timerTest.Interval = 1000 * _thoigianlambai;
                timerTest.Start();

                btn_Start.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void InsertTestResult(string enqnum, DateTime testdate, int listening, int reading, string madethi)
        {
            try
            {
                //MessageBox.Show(testdate.ToString());
                SQLHelper.ExecuteNonQuery(GlobalVariable.WSInfo.ConnectionString,
                                        String.Format(@"usp_InsertTestResult @enquirynumber = '{0}', @testdate = '{1}', @scoredirection = {2}, @scorelistening = {3}, 
                                                        @scorereading = {4}, @scorewriting = {5}, @madethi = '{6}', @ketqua = '{7}', @recommendedclass = '{8}'",
                                                        enqnum, testdate, 0, listening, reading, 0, madethi, "", ""));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region "Common method"
        //Common method

        private int AddABCD(Panel panel, int begin, int end, int y, string chimuc)
        {
            try
            {
                for (int i = begin; i <= end; i++)
                {
                    string index = (i < 10) ? "0" + i : i.ToString();
                    Panel groupABCD = new Panel();
                    groupABCD.Location = new Point(10, y + 20);
                    groupABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    groupABCD.AutoSize = true;
                    groupABCD.Name = "grp" + chimuc + index;
                    panel.Controls.Add(groupABCD);

                    Label lb = new Label();
                    lb.AutoSize = true;
                    lb.Location = new Point(10, 20);
                    lb.Text = i.ToString();
                    panel.Controls[groupABCD.Name].Controls.Add(lb);

                    RadioButton rda = new RadioButton();
                    rda.Location = new Point(lb.Location.X + 30, lb.Location.Y);
                    rda.Text = "a.";
                    rda.Name = "a" + chimuc + index;
                    rda.AutoSize = true;
                    panel.Controls[groupABCD.Name].Controls.Add(rda);
                    rda.CheckedChanged += new System.EventHandler(RadioButtonABCD_CheckedChanged);

                    RadioButton rdb = new RadioButton();
                    rdb.Location = new Point(rda.Location.X + 100, rda.Location.Y);
                    rdb.Text = "b.";
                    rdb.Name = "b" + chimuc + index;
                    rdb.AutoSize = true;
                    panel.Controls[groupABCD.Name].Controls.Add(rdb);
                    rdb.CheckedChanged += new System.EventHandler(RadioButtonABCD_CheckedChanged);

                    RadioButton rdc = new RadioButton();
                    rdc.Location = new Point(rdb.Location.X + 100, rda.Location.Y - 1);
                    rdc.Text = "c.";
                    rdc.Name = "c" + chimuc + index;
                    rdc.AutoSize = true;
                    panel.Controls[groupABCD.Name].Controls.Add(rdc);
                    rdc.CheckedChanged += new System.EventHandler(RadioButtonABCD_CheckedChanged);

                    RadioButton rdd = new RadioButton();
                    rdd.Location = new Point(rdc.Location.X + 100, rda.Location.Y - 2);
                    rdd.Text = "d.";
                    rdd.Name = "d" + chimuc + index;
                    rda.AutoSize = true;
                    panel.Controls[groupABCD.Name].Controls.Add(rdd);
                    rdd.CheckedChanged += new System.EventHandler(RadioButtonABCD_CheckedChanged);

                    y += 20;
                }
                return y;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
        }

        private Label AddLabel(Panel panel, string name, string text, Color color, int y)
        {
            try
            {
                Label lb = new Label();
                lb.Location = new Point(10, y);
                lb.AutoSize = true;
                lb.Name = name;
                lb.ForeColor = color;
                lb.Text = text;
                panel.Controls.Add(lb);
                return lb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
        #endregion

        #region "Get Value"
        private void RadioButtonABCD_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                RadioButton r = ((RadioButton)sender);
                string indexandstt = r.Name.ToString().Substring(1);
                string ndung = (r.Name.ToString().Substring(0, 1) == null) ? "" : r.Name.ToString().Substring(0, 1);
                this.pnl_Main.Controls["grp" + indexandstt].Tag = ndung;
                //MessageBox.Show(indexandstt + "/" + this.pnl_Main.Controls["grp" + indexandstt].Tag.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                TextBox t = (TextBox)sender;
                string indexandstt = t.Name.Substring(3);
                string ndung = (t.Text == null) ? "" : t.Text;
                this.pnl_Main.Controls["grp" + indexandstt].Tag = ndung;
                //MessageBox.Show(indexandstt + "/" + this.pnl_Main.Controls["grp" + indexandstt].Tag.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region "Get Result"
        private SqlDataReader GetResult(string chimuc)
        {
            try
            {
                SqlDataReader rd = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                                    String.Format(@"declare @chimuc varchar(10)
                                    set @chimuc = '{0}'
                                    select Substring(MaCauHoi, 4, LEN(MaCauHoi) - 3) as 'MaCauHoi', lower(DapAn) as 'DapAn'
                                    from TEST_Question 
                                    where MaDeThi = '{1}' 
                                    and SUBSTRING(MaCauHoi, 4, LEN(@chimuc)) = @chimuc", chimuc, _madethi));
                return rd;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
        #endregion

        #region "Mathing"
        private void Mathing()
        {
            try
            {
                SqlDataReader rd;
                int correctI1, correctI2, correctII, correctIII1, correctIII2, correctIV1, correctIV2, correctV1, correctV2, correctVI1, correctVI2, 
                    correctVII1, correctVII2, correctVIII1, correctVIII2, correctIX1, correctIX2;
                correctI1 = correctI2 = correctII = correctIII1 = correctIII2 = correctIV1 = correctIV2 = 
                correctV1 = correctV2 = correctVI1 = correctVI2 = correctVII1 = correctVII2 = correctVIII1 = correctVIII2 = correctIX1 = correctIX2 = 0;

                //I.1 Listen and circle the correct answer.
                rd = GetResult("I1");
                correctI1 = CountCorrect(rd);

                //I.2 Listen and answer true (T) or false (F).s0
                rd = GetResult("I2");
                correctI2 = CountCorrect(rd);

                //II. Level 1
                rd = GetResult("II0");
                correctII = CountCorrect(rd);

                //Level 2
                //III.1 A.  Match the sentences (1–7) with the answers (a–h).
                rd = GetResult("III1");
                correctIII1 = CountCorrect(rd);

                //III.2 B. Read the text and answer the questions.
                rd = GetResult("III2");
                correctIII2 = CountCorrect(rd);
                
                //Level 3
                //IV.1 A. Correct the mistakes with the correct adjective or adverb.
                rd = GetResult("IV1");
                correctIV1 = CountCorrect(rd);

                //IV.2 B.	Complete the text with the past form of the verbs below.
                rd = GetResult("IV2");
                correctIV2 = CountCorrect(rd);

                //Level 4
                //V.1 A. Complete the sentences with the correct form of the verbs.
                rd = GetResult("V1");
                correctV1 = CountCorrect(rd);

                //V.2 B.	Read and answer true (T) or false (F).
                rd = GetResult("V2");
                correctV2 = CountCorrect(rd);

                //Level 5
                //VI.1 A.	Write the exclamations under the correct heading.
                rd = GetResult("VI1");
                correctVI1 = CountCorrect(rd);

                //VI.2 B. Order the dialogue.
                rd = GetResult("VI2");
                correctVI2 = CountCorrect(rd);

                //Level 6
                //VII.1 A.	Correct the mistake in each sentence.
                rd = GetResult("VII1");
                correctVII1 = CountCorrect(rd);

                //VII.2 B.	Your friends have a lot of problems. Give them advice.
                rd = GetResult("VII2");
                correctVII2 = CountCorrect(rd);

                //Level 7
                //VIII.1 A.	Complete the sentences with one word in each gap.
                rd = GetResult("VIII1");
                correctVIII1 = CountCorrect(rd);

                //VIII.2 B	Complete the sentences with the correct form of the verbs in brackets.
                rd = GetResult("VIII2");
                correctVIII2 = CountCorrect(rd);

                //Level 8
                //IX.1 A.	Complete the sentences with one word in each gap.
                rd = GetResult("IX1");
                correctIX1 = CountCorrect(rd);

                //IX.2 B	Complete the sentences with the correct form of the verbs in brackets.
                rd = GetResult("IX2");
                correctIX2 = CountCorrect(rd);

                _listen = correctI1 + correctI2;
                _read = correctII + correctIII1 + correctIII2 + correctIV1 + correctIV2 + 
                            correctV1 + correctV2 + correctVI1 + correctVI2 + correctVII1 + correctVII2 + correctVIII1 + correctVIII2 + correctIX1 + correctIX2;

                lblResultListening.Text = "Listening: " + _listen;
                lblResultReading.Text = "Reading: " + _read;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private int CountCorrect(SqlDataReader rd)
        {
            try
            {
                int i = 0;
                while (rd.Read())
                {
                    if (this.pnl_Main.Controls["grp" + rd["MaCauHoi"].ToString()].Tag != null)
                    {
                        if (this.pnl_Main.Controls["grp" + rd["MaCauHoi"].ToString()].Tag.ToString() == rd["DapAn"].ToString().Trim())
                            i++;
                    }
                }
                return i;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
        }
        #endregion

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (slook_Enquiry.EditValue.ToString() != "" && slook_Enquiry.EditValue != null)
            {
                this.btn_Start.Enabled = false;
                slook_Enquiry.Enabled = false;
                this.pnl_Main.Enabled = true;
                this.btn_Finish.Enabled = true;
                LoadTime();
                LoadMusic(String.Format("{0}.mp3", _madethi));
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn tên.");
            }
        }

        private void btn_Finish_Click(object sender, EventArgs e)
        {
            try
            {
                btn_Finish.Enabled = false;
                slook_Enquiry.Enabled = false;
                axWindowsMediaPlayer1.Ctlcontrols.stop();
                timerTest.Stop();
                MessageBox.Show("Finish");
                Mathing();
                
                InsertTestResult(slook_Enquiry.EditValue.ToString(), Convert.ToDateTime(txtTestDate.EditValue.ToString()), _listen,
                                _read, _madethi);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void timerTest_Tick(object sender, EventArgs e)
        {
            if (progressBarTime.Value == 0)
            {
                timerTest.Stop();
                btn_Finish_Click(sender, e);
                MessageBox.Show("Test time has expired !", "Sorry");
                return;
            }
            progressBarTime.Value--;
            lblTimeLeft.Text = "Time left (minutes): " + progressBarTime.Value.ToString();
        }

        private void frmMT_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (btn_Finish.Enabled)
            {
                MessageBox.Show("Click <Finish button> to finish the test before closing form", "Placement Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Cancel = true;
            }
        }
    }
}
